package com.ibeetl.admin.console.web.business;

import com.ibeetl.admin.console.service.OrgConsoleService;
import com.ibeetl.admin.console.service.RechargeConsoleService;
import com.ibeetl.admin.console.service.RoleConsoleService;
import com.ibeetl.admin.console.service.UserConsoleService;
import com.ibeetl.admin.console.service.business.ScoreRuleService;
import com.ibeetl.admin.console.web.dto.RechargeExcelExportData;
import com.ibeetl.admin.console.web.query.RechargeQuery;
import com.ibeetl.admin.console.web.query.business.ScoreRuleQuery;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.annotation.Query;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.Recharge;
import com.ibeetl.admin.core.entity.business.ScoreRule;
import com.ibeetl.admin.core.file.FileItem;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.util.AnnotationUtil;
import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.jxls.common.Context;
import org.jxls.util.JxlsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 充值管理接口
 * 
 * @author xiandafu
 */
@Controller
public class ScoreRuleController {
	private final Log log = LogFactory.getLog(this.getClass());
	private static final String MODEL = "/admin/rule";

	@Autowired
	UserConsoleService userConsoleService;


	@Autowired
	CorePlatformService platformService;
	@Autowired
	private ScoreRuleService scoreRuleService;
	

   

	/* 页面 */

	@GetMapping(MODEL + "/index.do")
	@Function("scoreRule")
	public ModelAndView index() {
		ModelAndView view = new ModelAndView("/admin/business/rule/index.html");
		view.addObject("search", ScoreRuleQuery.class.getName());
		return view;
	}

	@GetMapping(MODEL + "/edit.do")
	@Function("scoreRule.edit")
	public ModelAndView edit(String openid) {
		ModelAndView view = new ModelAndView("/admin/business/rule/edit.html");
		ScoreRule scoreRule = scoreRuleService.queryRuleByOpenid(openid);
		view.addObject("scoreRule", scoreRule);
		return view;
	}

	/* Json */

	@PostMapping(MODEL + "/update.json")
	@Function("scoreRule.update")
	@ResponseBody
	public JsonResult update(@Validated(ValidateConfig.UPDATE.class) ScoreRule scoreRule) {
		CoreUser currentUser = platformService.getCurrentUser();
		String currentUserName = currentUser.getName();
		scoreRule.setUpdator(currentUserName);
		scoreRule.setCreator(currentUserName);
		scoreRule.setCreateTime(new Date());
		scoreRule.setUpdateTime(new Date());
		int success = scoreRuleService.doingRule(scoreRule);
		if (success>0) {
			this.platformService.clearFunctionCache();
			return JsonResult.success();
		} else {
			return JsonResult.failMessage("配置失败！");
		}
	}


	@PostMapping(MODEL + "/list.json")
	@Function("scoreRule.query")
	@ResponseBody
	public JsonResult<PageQuery<ScoreRule>> index(ScoreRuleQuery condtion) {

		PageQuery<ScoreRule> page = condtion.getPageQuery();
		scoreRuleService.queryByCondtion(page);
		return JsonResult.success(page);
	}

	@PostMapping(MODEL + "/list/condition.json")
	@Function("scoreRule.query")
	@ResponseBody
	public JsonResult<List<Map<String, Object>>> indexCondtion() {
		List<Map<String, Object>> list = AnnotationUtil.getInstance().getAnnotations(Query.class, ScoreRule.class);
		return JsonResult.success(list);
	}

		/* Json */

	@PostMapping(MODEL + "/delete.json")
	@Function("scoreRule.delete")
	@ResponseBody
	public JsonResult delete(String ids) {
		List<String> test = Arrays.asList(ids.split(","));
        System.out.println("[ID]"+ids);
		scoreRuleService.batchDelByOpenIds(test);
		return JsonResult.success();
	}

//	@PostMapping(MODEL + "/excel/export.json")
//	@Function("scoreRule.export")
//	@ResponseBody
//	public JsonResult<String> export(HttpServletResponse response,ScoreRuleQuery condtion) {
//		String excelTemplate ="excelTemplates/admin/business/rule/score_rule_collection_templat.xls";
//		PageQuery<Recharge> page = condtion.getPageQuery();
//		//取出全部符合条件的
//		page.setPageSize(Integer.MAX_VALUE);
//		page.setPageNumber(1);
//		page.setTotalRow(Integer.MAX_VALUE);
//		List<RechargeExcelExportData> recharges =rechargeConsoleService.queryExcel(page);
//		try(InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(excelTemplate)) {
//	        if(is==null) {
//	        	throw new PlatformException("模板资源不存在："+excelTemplate);
//	        }
//	        FileItem item = fileService.createFileTemp("score_rule_collection.xls");
//	        OutputStream os = item.openOutpuStream();
//	        Context context = new Context();
//            context.putVar("coinTotal", recharges);
//            JxlsHelper.getInstance().processTemplate(is, os, context);
//            //下载参考FileSystemContorller
//            return  JsonResult.success(item.getPath());
//	    } catch (IOException e) {
//			throw new PlatformException(e.getMessage());
//		}
//
//	}
//
	
	
	
	

}
